<?php
/**
* Datbase handle class
*
*/
class DB {
	var $link						= null;
	var	$connected			= false;
	var	$lastInsertID		= null;
	var $effectedCount	= null;
	var $db							= null;
	var $error 					= array();
	var $lastQueryHasError = false;
	var $showErrors 		= true;
	
	var $logInserts 		= false;
	
	
	function DB(){
		$this->connect( );
	}
	
	//Connection Functions
	function connect( ){ //array('host'=>, 'user'=>, 'pass'=>, 'db'=>);
		$this->link = @mysql_connect( DB_HOST, DB_USER, DB_PASS , true);
		if( !$this->link ){
			$this->addError($sql);
			return false;
		}
		
		$this->connected = true;
		$this->db = DB_NAME;
		if( $this->db ){ 
			if( !@mysql_select_db( $this->db , $this->link ) ){
				$this->addError($sql);
			}
		}
		
		return true;
	}
	function close(){
		if( $this->link == null ){
			$this->addError($sql);
			$this->connected = false;
			return true;
		}
		
		mysql_close( $this->link );
		$this->connected = false;
		return true;
	}
	
	function showErrors($showErrors){
		$this->showErrors = $showErrors;
	}
	//Query functions
	function query( $sql = null ){
		$this->lastQueryHasError = false;
		if( $sql == null ){ return false; }
		
		
		if( !$res = mysql_query( $sql, $this->link ) ){
			$this->addError($sql);			
			return false;
		}
		
		if( $id = mysql_insert_id( $this->link ) ){
			$this->lastInsertID	= $id;
		}
		
		if($count = mysql_affected_rows( $this->link )){
			$this->effectedCount = $count;
		}
		
		if( $this->logInserts ){
			$sqlCompressed = compressString(strtolower($sql));
			$pos = strpos($sqlCompressed,'insert');
			if( $pos !== false ){
				echo($sql."\n");
			}
		}
		
		return $res;
	}
	function getSingleResult( $sql = null ){
		if( $sql == null ){ return false; }
		
		$res = $this->query( $sql );
		$out = false;
		if( $res ){
			$out = @mysql_result( $res, 0 );
			mysql_free_result( $res );
		}
		
		return $out;
	}
	function getArray( $sql = null ){
		if( !$sql ){ return false; }
		
		$res = $this->query( $sql );
		
		if( !$res ){
			return false;
		}
		
		$out = array();
		while( $row = mysql_fetch_array($res, MYSQL_ASSOC ) ){
			$out[] = $row;
		}
		mysql_free_result( $res );
		
		return $out;
	}
	function getSingleArray( $sql = null ){
		if( $sql == null ){ return false; }
		
		$res = $this->query( $sql );
		
		if( !$res ){
			return false;
		}
		$out = '';
		while( $row = mysql_fetch_array($res, MYSQL_ASSOC ) ){
			$out = $row;
			break;
		}
		mysql_free_result( $res );
		
		return $out;
	}
	function getTableCols( $tbl = null ){
		if( $tbl == null ){
			return false;
		}
		
		$res = $this->query("DESCRIBE `$tbl`");
		if( !$res ){ return false; }
		
		$out = array();
		
		while($row = mysql_fetch_assoc( $res ) ){
			$out[] = $row['Field'];
		}
		
		mysql_free_result( $res );
		return $out;
	}
	function diffTables( $tbl1 = null, $tbl2 = null ){
		if( $tbl1 == null || $tbl2 == null ){
			return false;
		}
		
		$table1Cols		= $this->getTableCols( $tbl1 );
		$table2Cols		= $this->getTableCols( $tbl2 );
		
		return array_diff($table1Cols, $table2Cols);
	}
	function listTables(){
		$sql = "SHOW TABLES FROM `".$this->db."`";
		$res = $this->getArray($sql);
		$out = array();
		
		foreach($res as $key=>$val){
			$out[] = $val['Tables_in_'.$this->db];
		}
		return $out;
	}
	function describeTable($tbl = null){
		if( $tbl == null ){
			return false;
		}
		
		$res = $this->query("DESCRIBE `$tbl`");
		if( !$res ){ return false; }
		
		$out = array();
		
		while($row = mysql_fetch_assoc( $res ) ){
			$out[$row['Field']] = $row;
		}
		
		mysql_free_result( $res );
		return $out;
	}
	function fetchRow($resource) {
		$row = mysql_fetch_assoc($resource);
		
		if(mysql_error()) {
			echo 'Database Fetchrow Error - Database->fetchRow() : ' . mysql_error();
			return false;
		}
		
		return $row;
	}
	function numRows($resource) {
		return mysql_num_rows($resource);
	}
	function effectedRows(){
		return mysql_affected_rows( $this->link );
	}
	function updateViaArray( $update, $table, $where ){
		if( $update ){
			$updateArray = array();
			foreach($update as $key=>$val){
				if( is_null( $val ) || $val == 'NULL'){
					$updateArray[] = '`'.$key.'` = NULL';
				}else{
					$updateArray[] = '`'.$key.'` = "'.$val.'"';
				}
			}
		}
		
		$sql = 'UPDATE `'.$table.'` SET '.implode(',', $updateArray).' '.$where;
		$this->query( $sql );
		
		return ( $this->effectedRows() > 0 ) ? true : false;
	}
	function insertViaArray( $update, $table ){
		if( $update ){
			$updateArray = array();
			foreach($update as $key=>$val){
				$fields[] = '`'.$key.'`';
				if( is_null( $val ) || $val == 'NULL'){
					$values[] = 'NULL';
				}else{
					$values[] = '"'.$val.'"';
				}
			}
		}
		
		$sql = 'INSERT INTO `'.$table.'` ('.implode(',', $fields).') VALUES ('.implode(',', $values).') ';
		$this->query( $sql );
		
		return ( $this->effectedRows() > 0 ) ? true : false;
	}
	function clearErrors(){
		$this->error = array();
	}
	function addError($sql){
		$sqlError = mysql_error();
		$this->error[] =  array('sql'=>$sql, 'msg'=>$sqlError );
		$this->lastQueryHasError = true;
		if( $this->showErrors ){
			F::get()->Tpl()->addMessage('Database Error',"<pre>".$sqlError."</pre><pre>".str_replace("\t", '', $sql).'</pre>', 'error');
		}
	}
	
}
?>
